﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Common.Utils;
using Gao.Poker.Domain;

namespace Gao.Poker.Services
{
    public class UserService : BaseService
    {
        private static UserService _instance;

        public static UserService Instance()
        {
            return _instance ?? (_instance = new UserService());
        }

        public User Login(String userName,String password)
        {
            String pwd = EncryptHelper.Md5(password);
            var user = DbContext.User.FirstOrDefault(a => a.UserName.Equals(userName) && a.Password.Equals(pwd));
            return user;
        }

        public void Save(User user)
        {
            if (user == null) return;
            if(user.ID > 0)
            {
                DbContext.User.Attach(user);
                DbContext.Entry(user).State = EntityState.Modified;
            }
            else
            {
                DbContext.User.Add(user);
            }

            DbContext.SaveChanges();
        }
    }
}
